package com.tim.soundseechina;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.tim.framework.common.PageProperty;
import com.tim.soundseechina.entity.DataDict;
import com.tim.soundseechina.entity.InformationInfo;
import com.tim.soundseechina.service.IDataDictService;
import com.tim.soundseechina.service.IInformationInfoService;

public class Cache {
	private IDataDictService dataDictService;
	private IInformationInfoService informationInfoService;
	// 中文省份
	private static Map<Short, Integer> provinceMap = new HashMap<Short, Integer>();
	// 风景检索条件
	private static Map<String, Integer> queryMap = new HashMap<String, Integer>();
	// 文化分类
	private static Map<Short, Integer> cultureMap = new HashMap<Short, Integer>();
	// 朝代分类
	private static Map<Short, Integer> dynastyMap = new HashMap<Short, Integer>();
	// 民族分类
	private static Map<Short, Integer> nationalityMap = new HashMap<Short, Integer>();
	// 中英文切换
	private static Map<Integer, Integer> changeMap = new HashMap<Integer, Integer>();

	public void init() {
		PageProperty pp = new PageProperty();
		pp.addSearch("status=?", (short) 0);
		pp.setOrderString("groupId,seq,id");
		// 查询全部
		pp.setNpagesize(0);
		List<DataDict> provinceList = dataDictService.getList(pp);
		Map<String, Integer> tempMap = new HashMap<String, Integer>();
		for (int i = 0; i < provinceList.size(); i++) {
			DataDict dataDict = provinceList.get(i);
			// 中文类型
			if (dataDict.getLanType().equals("cn")) {
				if (dataDict.getGroupId() == 1) {
					provinceMap.put(dataDict.getSeq(), dataDict.getId());
					tempMap.put(
							dataDict.getGroupId() + "_" + dataDict.getSeq(),
							dataDict.getId());
				} else if (dataDict.getGroupId() == 12) {
					queryMap.put(dataDict.getGroupName(), dataDict.getId());
					tempMap.put(
							dataDict.getGroupId() + "_" + dataDict.getSeq(),
							dataDict.getId());
				} else if (dataDict.getGroupId() == 5) {
					cultureMap.put(dataDict.getSeq(), dataDict.getId());
				} else if (dataDict.getGroupId() == 11) {
					dynastyMap.put(dataDict.getSeq(), dataDict.getId());
				} else if (dataDict.getGroupId() == 4) {
					nationalityMap.put(dataDict.getSeq(), dataDict.getId());
				}
			}
			if (dataDict.getGroupId() == 12) {
				if (dataDict.getLanType().equals("en")) {
					changeMap.put(tempMap.get(dataDict.getGroupId() + "_"
							+ dataDict.getSeq()), dataDict.getId());
				}
			}
		}
		System.out.println("初始化完成");
	}

	public Map<Short, Integer> getProvinceMap() {
		return provinceMap;
	}

	public Map<String, Integer> getQueryMap() {
		return queryMap;
	}

	public Map<Short, Integer> getCultureMap() {
		return cultureMap;
	}

	public Map<Short, Integer> getDynastyMap() {
		return dynastyMap;
	}

	public IDataDictService getDataDictService() {
		return dataDictService;
	}

	public void setDataDictService(IDataDictService dataDictService) {
		this.dataDictService = dataDictService;
	}

	public IInformationInfoService getInformationInfoService() {
		return informationInfoService;
	}

	public void setInformationInfoService(
			IInformationInfoService informationInfoService) {
		this.informationInfoService = informationInfoService;
	}

	public static void main(String[] args) {
		String str = "<a disabled=\"disabled\" style=\"margin-right:5px;\">&lt;&lt;</a><a disabled=\"disabled\" style=\"margin-right:5px;\">&lt;</a><span class=\"current\" style=\"megas512\" style=\"margin-right:5px;\">10</a><span style=\"margin-right:5px;\"><a class=\"megas512\" href=\"culture_classic.aspx?page=11\">...</a></span><a class=\"megas512\" href=\"culture_classic.aspx?page=2\" style=\"margin-right:5px;\">&gt;</a><a class=\"megas512\" href=\"culture_classic.aspx?page=24\" style=\"margin-right:5px;\">&gt;&gt;</a>";
		System.out.println(str.substring(str.lastIndexOf("page=") + 5, str
				.lastIndexOf("\" style")));
	}

	public static Map<Short, Integer> getNationalityMap() {
		return nationalityMap;
	}

	public InformationInfo getInformation(String title) {
		PageProperty pp = new PageProperty();
		pp.addSearch("title=?", title);
		pp.setNpagesize(1);
		List<InformationInfo> infoList = informationInfoService.getList(pp);
		if (infoList.size() > 0) {
			return infoList.get(0);
		}
		return null;
	}

	public Integer getCn2EnId(Integer cnId) {
		return changeMap.get(cnId);
	}
}
